
package com.epam.android.training.app.activities;

import com.epam.android.training.app.R;
import com.epam.android.training.app.db.TestsDb;
import com.epam.android.training.app.db.entities.TestQuestion;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import java.util.List;

/**
 * Test's questions activity.
 * 
 * @author Natallia_Kukharchyk
 */
public class TestQuestionActivity extends ActivityWithMenu {

  private List<TestQuestion> questionList;

  private Long testId;

  private TestsDb db;

  private int currentQuestion = 0;

  private int totalScore = 0;

  private TextView title;

  private RadioGroup group;

  private Button button;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testanswer);
    Intent intent = getIntent();
    testId = intent.getLongExtra(TestListActivity.TEST_ID, -1);
    questionList = getTestsQuestions(testId);
    group = (RadioGroup) findViewById(R.id.radio_group);
    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId >= 0) {
          button.setEnabled(true);
        }
      }
    });

    title = (TextView) findViewById(R.id.question);
    button = (Button) findViewById(R.id.confirm);
    button.setEnabled(false);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        int rightAnsewer = questionList.get(currentQuestion).getRightAnswer();
        if (rightAnsewer == group.getCheckedRadioButtonId()) {
          totalScore++;
        }
        currentQuestion++;
        group.removeAllViews();
        if (currentQuestion < questionList.size()) {
          populateView(currentQuestion);
        } else {
          TextView score = (TextView) findViewById(R.id.score);
          score.setText(getString(R.string.total_score_text) + totalScore);
          if (null != db) {
            db.writeScoreToDb(testId, totalScore);
          }
        }
        button.setEnabled(false);
        group.clearCheck();
      }
    });
    if (null != questionList) {
      populateView(currentQuestion);
    }

  }

  private List<TestQuestion> getTestsQuestions(Long id) {
    db = new TestsDb(getApplicationContext());
    return db.getQuestionsForTest(id);
  }

  private void populateView(int id) {
    TestQuestion question = questionList.get(id);
    title.setText(question.getTitle());
    populateAnswers(question.getAnswers());
  }

  private void populateAnswers(List<String> answers) {
    int i = 0;
    for (String answer : answers) {
      RadioButton radio = new RadioButton(getApplicationContext());
      radio.setText(answer);
      radio.setId(i);
      group.addView(radio);
      i++;
    }
  }

}
